package com.blah.gwtgames.client.common.ui;

import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

public class ProgressBar extends Widget {
	private Element track;
	private Element filler;
	private float value;
	
	public ProgressBar()
	{
		value = 0.0f;
		
		track = DOM.createDiv();
		DOM.setStyleAttribute(track, "backgroundColor", "#aaa");
		DOM.setStyleAttribute(track, "width", "100px");
		DOM.setStyleAttribute(track, "height", "8px");

		filler = DOM.createDiv();
		DOM.setStyleAttribute(filler, "backgroundColor", "#888");
		DOM.setStyleAttribute(filler, "height", "8px");
		DOM.appendChild(track, filler);

		setElement(track);
		setStyleName("gwt-ProgressBar");
	}
	
	public void setValue(float value)
	{
		this.value = value;
		
		int trackWidth = DOM.getIntStyleAttribute(track, "width");
		int fillerWidth = (int)(trackWidth * value);
		
		DOM.setStyleAttribute(filler, "width", fillerWidth + "px");
	}
	
	public float getValue() {return value;}
	
	public void setWidth(String width)
	{
		super.setWidth(width);
		setValue(value); // Resizes the filler element
	}
}
